<template>
  <div class="login-page">
    <el-card class="form-container">
      <h1 class="title">红色党建预约平台登录</h1>
      <el-form ref="formRef" :model="formModel" :rules="formRules">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formModel.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="formModel.password"></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="navigateToRegister">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { userLoginService } from '@/api/user.js'
import { useTokenStore } from '@/stores/token.js'

const router = useRouter()
const tokenStore = useTokenStore()

const formRef = ref()
const formModel = reactive({
  username: '',
  password: ''
})
const formRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  ]
})

const login = async () => {
  try {
    await formRef.value.validate()
    let result = await userLoginService(formModel)
    ElMessage.success(result.msg ? result.msg : '登录成功')
    tokenStore.setToken(result.data.token)
    if (result.data.role === 'ADMIN') {
      router.push('/admin')
    } else {
      router.push('/')
    }
  } catch (error) {
    ElMessage.error('登录失败，请检查输入')
  }
}

const navigateToRegister = () => {
  router.push('/register')
}

const reset = () => {
  formRef.value.resetFields()
}
</script>

<style lang="scss" scoped>
/* 样式 */
.login-page {
  height: 100vh;
  background-image: url('@/assets/login_bg.jpg'); 
  background-color: #f0f2f5;
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;

  .form-container {
    width: 400px;
    max-width: 100%;
    padding: 40px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);

    .title {
      font-size: 24px;
      margin-bottom: 30px;
      text-align: center;
      color: #333; /* 可以根据需要调整颜色 */
    }

    .el-form {
      display: flex;
      flex-direction: column;
      justify-content: center;
      user-select: none;

      .el-form-item {
        margin-bottom: 20px;
      }

      .el-input {
        width: 100%;
      }

      .button {
        width: 100%;
        margin-top: 20px;
      }

      .flex {
        width: 100%;
        display: flex;
        justify-content: space-between;
        margin-bottom: 20px;
      }

      .el-checkbox {
        margin-right: 10px;
      }

      .el-link {
        font-size: 14px;
      }
    }
  }
}
</style>